<template>
  <div>
    <jiazai />
    <div class="login-box">
      <h2>登录</h2>
      <form @submit.prevent>
        <div class="form-control">
          <label for="username">用户名</label>
          <input
            type="text"
            v-model="username"
            id="username"
            @input="validateUsername"
          />
          <span  class="error-message">{{
            usernameError
          }}</span>
        </div>
        <div class="form-control">
          <label for="password">密码</label>
          <input
            type="password"
            v-model="password"
            id="password"
            @input="validatePassword"
          />
          <span  class="error-message">{{
            passwordError
          }}</span>
        </div>
        <div class="form-control">
          <button type="submit" @click="login" class="half-width">登录</button>
          <button type="button" @click="goToRegister" class="half-width">
            注册
          </button>
        </div>
      </form>
    </div>
  </div>
</template>
    
    <script>
import jiazai from "../zi/加载中.vue";
import { provide, ref, computed, inject,onMounted } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

export default {
  components: { jiazai },
  setup() {
    const isLoading = ref(false);
    provide("isLoading", isLoading);
    const router = useRouter();
    const store = inject("store");
    const count1 = computed(() => store.state.zhanghui);
    const username = ref("");
    const password = ref("");

    const usernameError = ref("");
    const passwordError = ref("");

    function validateUsername() {
      if (username.value.trim().length < 3) {
        usernameError.value = "用户名长度不能少于3位";
      } else {
        usernameError.value = "";
      }
    }

    function validatePassword() {
      if (password.value.trim().length < 4) {
        passwordError.value = "密码长度不能少于4位";
      } else {
        passwordError.value = "";
      }
    }

    function login() {
      const user = count1.value.find((u) => u.username === username.value);
      if (user && user.password == password.value) {
        // 发送登录请求
        isLoading.value = true;
        const timerId = setTimeout(() => {
          ElMessage({
            type: "success",
            message: "登录成功",
          });
          store.commit('gaiapprrr',true)
          isLoading.value = false;
          store.commit('datausernamer',username.value)
          router.push({ name: "about", query: { id: 1 } });
        }, 1000);
      } else {
        ElMessage({
          type: "error",
          message: "请正确填写登录信息",
        });
      }
    }

    function goToRegister() {
      isLoading.value = true;
      const timerId = setTimeout(() => {
        ElMessage({
          type: "success",
          message: "请注册:用户名长度不能少于3位,密码长度不能少于4位",
        });
        isLoading.value = false;
        router.push("/Register");
      }, 1000);
    }

    return {
      username,
      password,
      usernameError,
      passwordError,
      validateUsername,
      validatePassword,
      login,
      goToRegister,
    };
  },
};
</script>
    
    <style scoped>
input:focus {
  border-color: #8dbdff;
  box-shadow: 0 0 0 2px #8dbdff;
}
.login-box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 400px;
  padding: 40px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
  z-index: 999;
}

.login-box h2 {
  text-align: center;
  margin-bottom: 30px;
}

.form-control {
  margin-bottom: 20px;
}

.form-control label {
  display: block;
  margin-bottom: 5px;
}

.form-control input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
  outline: none;
}

.error-message {
  height: 20px;
  color: red;
  font-size: 12px;
}

.form-control {
  margin-bottom: 20px;
  display: flex;
  flex-wrap: wrap;
}

.half-width {
  width: 50%;
}
button[type="button"],
button[type="submit"] {
  width: 50%;
  padding: 10px;
  background-color: #fff;
  color: #161414;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s ease;
}
button[type="submit"]:hover,
button[type="button"]:hover {
  background-color: hsl(180, 80%, 70%);
}
</style>
